import regular from "./reg";

const parseUrl = (url: string) => {
    let r = {
        protocol: /([^\/]+:)\/\/(.*)/i,
        host: /(^[^\:\/]+)((?:\/|:|$)?.*)/,
        port: /\:?([^\/]*)(\/?.*)/,
        pathname: /([^\?#]+)(\??[^#]*)(#?.*)/
    } as any;
    let tmp, res = {} as any;
    res[`href`] = url;
    for (let p in r) {
        tmp = r[p].exec(url);
        res[p] = tmp[1];
        url = tmp[2];
        if (!url) {
            url = `/`;
        }
        if (p === `pathname`) {
            res[`pathname`] = tmp[1];
            res[`search`] = tmp[2];
            res[`hash`] = tmp[3];
        }
    }
    return res;
}

export default (url: string = ``, big?: boolean) => {
    if (!regular.url.test(url)) {
        return url
    }
    if (url.includes(`default`)) {
        return url;
    }
    let urlObj = parseUrl(url);
    let {
        protocol,
        pathname,
        host
    } = urlObj;
    return `${protocol}//${host}/${!!big ? `b` : `s`}${pathname}`;
}